//
//  DHDoctorApplyViewController.m
//  DHKuaiYi
//
//  Created by 刘鹏飞 on 2016/11/13.
//  Copyright © 2016年 30-陈辉华. All rights reserved.
//

#import "DHDoctorApplyViewController.h"
#import "DHDoctorModel.h"
#import "DHDoctorTableViewCell.h"
#import "NetworkTool.h"
#import <YYModel.h>
#import <SVProgressHUD.h>
#import <MJRefresh.h>
#import "DHDoctorDetailsViewController.h"


static NSString *reuseID = @"DHDoctorCell";

@interface DHDoctorApplyViewController ()<UITableViewDataSource,UITableViewDelegate>

/*---tabelView---*/
@property(nonatomic,strong)UITableView *tableView;

/*---模型数组---*/
@property (nonatomic,strong)NSArray *doctors;

@end

@implementation DHDoctorApplyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self request];
}

#pragma mark - 网络请求

/**
 网络请求
 */
-(void)request {
    NSString *URLStr = @"http://iosapi.itcast.cn/doctor/matchDoctors.json.php";
    
    NSDictionary *parameters = @{@"ci1_id":@1,@"ci2_id":@3,@"ci3_id":@3,@"diagnosis_type":@0,@"page_size":@15,@"is_confirmed":@1,@"user_id":@1000089, @"page":@1,@"has_diagnosis":@2};
    
    [[NetworkTool sharedTool] POSTWithURLStr:URLStr parameters:parameters success:^(id responseObject) {
        if (responseObject != nil) {
            //调用第三方弹出提示框
            [SVProgressHUD showWithStatus:@"正在加载中...."];
            [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeNone];
            delaysuc(1.0);
            delay(2.0);
            //结束刷新
            delayUpdata(1.0);
            NSArray *dictArr = responseObject[@"data"];
            
            self.doctors = [NSArray yy_modelArrayWithClass:[DHDoctorModel class] json:dictArr];
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                
                [self.tableView reloadData];
            }];
        }
    } failed:^(NSError *error) {
        //调用第三方弹出提示框
        [SVProgressHUD showWithStatus:@"您的网络不给力,加载失败!"];
        [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
        delay(1.0);
        [self.tableView.mj_header endRefreshing];

    }];
}

#pragma mark - 创建子控件布局

/**
 设置子控件
 */
-(void)xhhSetupSubviews {
    UITableView *tabelView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    
    self.tableView = tabelView;
    [self.view addSubview:tabelView];
    
    //设置代理
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    //设置属性
    self.tableView.rowHeight = 86;
    self.tableView.showsVerticalScrollIndicator = NO;
    
    [self.tableView registerClass:[DHDoctorTableViewCell class] forCellReuseIdentifier:reuseID];
    //设置下拉刷新
    MJRefreshStateHeader *header = [MJRefreshStateHeader headerWithRefreshingBlock:^{
        [self request];
    }];
    header.lastUpdatedTimeLabel.hidden = YES;
    
    self.tableView.mj_header = header;
    
    //设置上拉刷新
    MJRefreshAutoStateFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self request];
    }];
    footer.refreshingTitleHidden = YES;
    self.tableView.mj_footer = footer;
}

/**
 设置顶部标题
 */
-(void)xhhSetupNavigationItems {
    
    [super xhhSetupNavigationItems];
    self.navigationItem.title = @"名医申请";
}

#pragma mark - 实现代理方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    DHDoctorDetailsViewController *vc = [[DHDoctorDetailsViewController alloc] init];
    
    vc.doctorInfo = self.doctors[indexPath.row];
    
    [self.navigationController pushViewController:vc animated:YES];
    
}

#pragma mark - 实现数据源方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.doctors.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    DHDoctorModel *doctor = self.doctors[indexPath.row];
    DHDoctorTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseID];
    cell.doctor = doctor;
    
    return cell;
    
}


@end
